<template>
  <div class="energy-system-container">
    <div class="section">
      <h2 class="section-title">计算参数</h2>

      <div class="grid">
        <div class="card">
          <label class="card-label">规划年限</label>

          <div class="card-content">
            <input
              type="number"
              v-model="environment.carbonPrice"
              @input="handleCarbonPriceInput"
              class="input small"
              min="0"
              max="5"
            />
            <span class="unit">(N≤5)</span>
            <span v-if="showMaxWarning" class="error-message">输入值不能大于5</span>
          </div>

          <button class="btn" @click="submitAllDeviceParams">
            <span class="btn-text">提交设备参数</span>

          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

// 数据对象
const environment = ref({
  carbonPrice: 0,
  carbonLimit: 100000
})

// 最大值提示
const showMaxWarning = ref(false)

// 输入限制处理
const handleCarbonPriceInput = (event: Event) => {
  const inputElement = event.target as HTMLInputElement;
  let value = parseFloat(inputElement.value);

  if (value > 5) {
    value = 5;
    showMaxWarning.value = true;
  } else {
    showMaxWarning.value = false;
  }

  environment.value.carbonPrice = value;
  inputElement.value = value.toString();
}

// 提交参数
const submitAllDeviceParams = async () => {
  try {
    const value = environment.value.carbonPrice;
    console.log(value);
    const response = await axios.put(`http://localhost:8080/pland/insert/${value}`);
    console.log(response.data.code);
    if(response.data.code === 0){
      ElMessage.success('设备参数提交成功');
    }else{
      ElMessage.success(response.data.message);
    }

  } catch (err) {
    ElMessage.error('提交失败，请检查网络或数据');
    console.error(err);
  }
}
</script>

<style scoped>
.error-message {
  color: #ef4444;
  font-size: 12px;
  margin-left: 8px;
}
.energy-system-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}
.section-title {
  color: #4c1d95;
  margin-bottom: 24px;
  font-size: 24px;
  font-weight: 700;
  padding-bottom: 12px;
  border-bottom: 2px solid #7c3aed;
}
.section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}
.card {
  background-color: white;
  padding: 16px;
  border-radius: 8px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}
.card-label {
  display: block;
  font-weight: 500;
  color: #475569;
  margin-bottom: 8px;
  font-size: 14px;
}
.card-content {
  display: flex;
  align-items: center;
  gap: 8px;
}
.input {
  padding: 8px 12px;
  border: 1px solid #053676;
  border-radius: 6px;
  width: 100px;
  text-align: right;
  font-size: 14px;
  background: white;
  color: #1e293b;
}
.input.small {
  width: 80px;
}
.input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.2);
}
.unit {
  font-weight: 500;
  color: #64748b;
  font-size: 13px;
  white-space: nowrap;
}
.btn {
  background-color: #7c3aed;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
  margin-top: 8px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.btn:hover {
  background-color: #6d28d9;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}
.btn-text {
  font-size: 13px;
  line-height: 1.2;
}
.file-name {
  color: rgba(255, 255, 255, 0.8);
  font-size: 11px;
  font-style: italic;
  margin-top: 2px;
}
@media (max-width: 768px) {
  .energy-system-container {
    padding: 20px;
    border-radius: 0;
  }
  .grid {
    grid-template-columns: 1fr;
  }
  .input {
    width: 100%;
  }
}
</style>
